package com.wangsd.common.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

@Component
public class OssUtils {

    @Value("${oss_endpoint}")
    private String endpoint;

    @Value("${oss_accessKeyId}")
    private String accessKeyId;

    @Value("${oss_accessKeySecret}")
    private String accessKeySecret;

    @Value("${oss_bucketName}")
    private String bucketName;

    @Value("${oss_imgUrl}")
    private String imgUrl;


    /**
     * 阿里云上传图片
     *
     * @param filename
     * @param file
     * @return
     */
    public String uploadImg(String filename, MultipartFile file) throws IOException {
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        ossClient.putObject(bucketName, filename, file.getInputStream());
        ossClient.shutdown();
        return imgUrl + "/" + filename;
    }

    /**
     * 上传文件
     *
     * @param filename
     * @param inputStream
     * @return
     */
    public String uploadFile(String filename, InputStream inputStream) {
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        ossClient.putObject(bucketName, filename, inputStream);
        ossClient.shutdown();
        return imgUrl + "/" + filename;
    }

    /**
     * 上传文件
     *
     * @param filename
     * @param str
     * @return
     */
    public String uploadFile(String filename, String str) {
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        ossClient.putObject(bucketName, filename, new ByteArrayInputStream(str.getBytes()));
        ossClient.shutdown();
        return imgUrl + "/" + ApplicationUtils.encodeURL(filename);
    }

    /**
     * 上传文件
     *
     * @param filename
     * @param file
     * @return
     */
    public String uploadFile(String filename, File file) {
        System.out.println(DateUtils.getCurDatetime());
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        ossClient.putObject(bucketName, filename, file);
        ossClient.shutdown();
        System.out.println(DateUtils.getCurDatetime());
        return imgUrl + "/" + ApplicationUtils.encodeURL(filename);
    }

    public String uploadFile(String addPath,String filename, File file) {
        System.out.println(DateUtils.getCurDatetime());
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        ossClient.putObject(bucketName, addPath+"/"+filename, file);
        ossClient.shutdown();
        System.out.println(DateUtils.getCurDatetime());
        return imgUrl + "/"+addPath+"/" + ApplicationUtils.encodeURL(filename);
    }

    public void deleteFile(String filename) {
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        ossClient.deleteObject(bucketName, filename);
        ossClient.shutdown();
    }
}

